package com.steak.feiyan.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.steak.feiyan.utils.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author 刘牌
 * @Date 2020/2/17 0017 19:13
 * @Version 1.0
 */
@RestController
@CrossOrigin(allowedHeaders = "*")
public class DataController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 树状图展示
     * @param city
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("charts")
    public JSONObject charts(@RequestParam String city) throws IOException, URISyntaxException {
        String URI = "https://api.inews.qq.com/newsqa/v1/query/pubished/daily/list";
        JSONObject jsonObject = HttpClient.httpGet(URI, "province", city);
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("data").toString());
        List<String> listData = new LinkedList<>();
        List<Integer> listPeople = new LinkedList<>();
        JSONObject jsonObjectData = new JSONObject();
        for (int i = 0 ; i < jsonArray.size() ; i++){
            JSONObject jsonObject1 = JSONObject.parseObject(jsonArray.get(i).toString());
            listData.add(jsonObject1.get("date").toString().substring(1));
            listPeople.add((Integer) jsonObject1.get("confirm"));
            jsonObjectData.put("date",listData);
            jsonObjectData.put("confirm",listPeople);
        }
        return jsonObjectData;
    }

    /**
     * 中国地图展示数据
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("chinaCharts")
    public Object chinaCharts() throws IOException, URISyntaxException {
        ArrayList<JSONObject> list = new ArrayList<JSONObject>();
        Boolean hasKey = redisTemplate.hasKey("chartsData");
        //设置超时时间
        redisTemplate.expire("chartsData",5, TimeUnit.HOURS);
        if (hasKey){
            Object chartsData = redisTemplate.opsForValue().get("chartsData");
            System.out.println(chartsData);
            return chartsData;
        }
        else {
            String[] chinaArray = {"湖北","北京","新疆","甘肃","宁夏","陕西","四川","西藏","重庆","云南","贵州","湖南","江西","青海","浙江","福建","山西","河南","山东","江苏","广西","广东","安徽","河北","内蒙古","黑龙江","吉林","辽宁","海南","台湾","香港","澳门"};
            String URI = "https://api.inews.qq.com/newsqa/v1/query/pubished/daily/list";
            for (int i = 0 ; i < chinaArray.length ; i++){
                JSONObject jsonObject = HttpClient.httpGet(URI, "province", chinaArray[i]);
                JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("data").toString());
                for (int j = 0 ; j < jsonArray.size() ; j++){
                    if (jsonArray.size()-1 == j){
                        JSONObject jsonObject1 = JSONObject.parseObject(jsonArray.get(j).toString());
                        JSONObject newJ = new JSONObject();
                        newJ.put("name",jsonObject1.get("province"));
                        newJ.put("value",jsonObject1.get("confirm"));
                        list.add(newJ);
                    }
                }
            }
            redisTemplate.opsForValue().set("chartsData",list);
            return list;
        }
    }

    /**
     * 表格数据
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("tableData")
    public Object tableData() throws IOException, URISyntaxException {
        ArrayList<JSONObject> list = new ArrayList<JSONObject>();
        Boolean hasKey = redisTemplate.hasKey("tableData");
        //设置超时时间
        redisTemplate.expire("tableData",5, TimeUnit.HOURS);
        if (hasKey){
            Object chartsData = redisTemplate.opsForValue().get("tableData");
            System.out.println(chartsData);
            return chartsData;
        }
        else {
            String[] chinaArray = {"湖北","北京","新疆","甘肃","宁夏","陕西","四川","西藏","重庆","云南","贵州","湖南","江西","青海","浙江","福建","山西","河南","山东","江苏","广西","广东","安徽","河北","内蒙古","黑龙江","吉林","辽宁","海南","台湾","香港","澳门"};
            String URI = "https://api.inews.qq.com/newsqa/v1/query/pubished/daily/list";
            for (int i = 0 ; i < chinaArray.length ; i++){
                JSONObject jsonObject = HttpClient.httpGet(URI, "province", chinaArray[i]);
                JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("data").toString());
                for (int j = 0 ; j < jsonArray.size() ; j++){
                    if (jsonArray.size()-1 == j){
                        JSONObject jsonObject1 = JSONObject.parseObject(jsonArray.get(j).toString());
                        list.add(jsonObject1);
                    }
                }
            }
            System.out.println("tableData  "+list);
            redisTemplate.opsForValue().set("tableData",list);
            return list;
        }
    }

    /**
     * 疫情速报
     * @param provinceCode
     * @param request
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("news")
    public JSONObject news(@RequestParam String provinceCode, HttpServletRequest request) throws IOException, URISyntaxException {
        String URI = "https://api.dreamreader.qq.com/news/v1/province/news/list";
        JSONObject jsonObject = HttpClient.httpGet(URI, "province_code", provinceCode);
        return jsonObject;
    }
}
